package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.repository;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iwhalecloud.citybrain.flow.platform.manager.application.utils.StringUtils;
import com.iwhalecloud.citybrain.flow.platform.manager.common.ResponseResult;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.*;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowChannelRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowChannel;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowChannelExample;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.mapper.FlowChannelDAO;
import org.apache.commons.collections4.CollectionUtils;
import org.flowable.engine.common.impl.cfg.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Repository
public class FlowChannelRepositoryImpl implements FlowChannelRepository {

    @Autowired
    private FlowChannelDAO flowChannelDAO;

    @Autowired
    private IdGenerator idGenerator;

    @Override
    public ResponseResult list(FlowChannelVo flowChannelVo) {
        FlowChannelExample flowChannelExample = new FlowChannelExample();
        if (StringUtils.isNotEmpty(flowChannelVo.getKeyWord())) {
            String searchText = "%" + flowChannelVo.getKeyWord() + "%";
            flowChannelExample.createCriteria().andChannelNameLike(searchText);
        }
        PageHelper.startPage(flowChannelVo.getPageNum(), flowChannelVo.getPageSize());
        List<FlowChannel> flowChannels = flowChannelDAO.selectByExample(flowChannelExample);
        PageInfo<FlowChannel> pageInfo = new PageInfo<>(flowChannels);

        List<FlowChannelInfo> flowChannelInfos = new ArrayList<>(flowChannels.size());
        for (FlowChannel flowChannel : flowChannels) {
            FlowChannelInfo flowChannelInfo = new FlowChannelInfo();
            BeanUtils.copyProperties(flowChannel, flowChannelInfo);
            flowChannelInfos.add(flowChannelInfo);
        }
        ResponseResult responseResult = ResponseResult.success(flowChannelInfos);
        responseResult.setTotalCount((int) pageInfo.getTotal());
        responseResult.setPageNum(flowChannelVo.getPageNum());
        responseResult.setPageSize(flowChannelVo.getPageSize());
        return responseResult;
    }

    @Override
    public String add(FlowChannelAddVo flowChannelAddVo) {
        FlowChannel flowChannel = new FlowChannel();
        BeanUtils.copyProperties(flowChannelAddVo, flowChannel);
        flowChannel.setCreateTime(new Date());
        flowChannel.setId(idGenerator.getNextId());
        flowChannelDAO.insert(flowChannel);
        return flowChannel.getId();
    }

    @Override
    public Integer edit(FlowChannelEditVo flowChannelEditVo) {
        FlowChannel flowChannel = new FlowChannel();
        BeanUtils.copyProperties(flowChannelEditVo, flowChannel);
        flowChannel.setUpdateTime(new Date());
        return flowChannelDAO.updateByPrimaryKeySelective(flowChannel);
    }

    @Override
    public Integer delete(String id) {
        return flowChannelDAO.deleteByPrimaryKey(id);
    }

    @Override
    public FlowChannelInfo detail(String id) {
        FlowChannel flowChannel = flowChannelDAO.selectByPrimaryKey(id);
        FlowChannelInfo flowChannelInfo = new FlowChannelInfo();
        BeanUtils.copyProperties(flowChannel, flowChannelInfo);
        return flowChannelInfo;
    }

    @Override
    public Boolean isTemplateExist(String type) {
        FlowChannelExample flowChannelExample = new FlowChannelExample();
        flowChannelExample.createCriteria().andChannelTypeEqualTo(type);
        List<FlowChannel> flowChannels = flowChannelDAO.selectByExample(flowChannelExample);
        if (CollectionUtils.isEmpty(flowChannels)) {
            return false;
        }
        return true;
    }

    @Override
    public Integer startStatus(FlowChannelStatusVo flowChannelStatusVo) {
        FlowChannel flowChannel = new FlowChannel();
        BeanUtils.copyProperties(flowChannelStatusVo, flowChannel);
        flowChannel.setUpdateTime(new Date());
        return flowChannelDAO.updateByPrimaryKeySelective(flowChannel);
    }

}
